package com.dandandog.framework.oss.config;

import com.dandandog.framework.oss.config.properties.OssProperties;
import com.dandandog.framework.oss.service.OssFileService;
import com.dandandog.framework.oss.service.impl.MinioServiceImpl;
import com.dandandog.framework.oss.service.impl.QiniuServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author JohnnyLiu
 */
@Configuration
@AllArgsConstructor
@EnableConfigurationProperties({OssProperties.class})
@ConditionalOnProperty(prefix = "fast-code.oss", value = {"enabled"}, matchIfMissing = true)
public class OssConfig {

    private final OssProperties properties;

    @Bean
    public OssFileService fileService() {
        OssFileService service;
        switch (properties.getType()) {
            case Minio:
                service = new MinioServiceImpl(properties);
                break;
            case Qiniu:
                service = new QiniuServiceImpl(properties);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + properties.getType());
        }

        return service;
    }


}
